package com.phiture.erp.produce.web.controller.admin;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.util.collection.MapUtils;
import cn.iocoder.yudao.module.system.api.dept.DeptApi;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
import com.phiture.erp.produce.core.pojo.vo.ErpProduceProcessPageReqVO;
import com.phiture.erp.produce.core.pojo.vo.ErpProduceProcessRespVO;
import com.phiture.erp.produce.core.pojo.vo.ErpProduceProcessSaveReqVO;
import com.phiture.erp.produce.core.service.ErpProduceProcessService;
import com.phiture.erp.produce.dal.entity.ErpProduceProcessDO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMultiMap;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;

@Tag(name = "管理后台 - 工艺工序")
@RestController
@RequestMapping("/erp/produce-process")
@Validated
public class ErpProduceProcessController {

    @Resource
    private ErpProduceProcessService produceProcessService;
    @Resource
    private DeptApi deptApi;

    @PostMapping("/create")
    @Operation(summary = "创建工艺工序")
    @PreAuthorize("@ss.hasPermission('erp:produce-process:create')")
    public CommonResult<Long> createProduceProcess(@Valid @RequestBody ErpProduceProcessSaveReqVO createReqVO) {
        return success(produceProcessService.createProduceProcess(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新工艺工序")
    @PreAuthorize("@ss.hasPermission('erp:produce-process:update')")
    public CommonResult<Boolean> updateProduceProcess(@Valid @RequestBody ErpProduceProcessSaveReqVO updateReqVO) {
        produceProcessService.updateProduceProcess(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除工艺工序")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:produce-process:delete')")
    public CommonResult<Boolean> deleteProduceProcess(@RequestParam("id") Long id) {
        produceProcessService.deleteProduceProcess(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得工艺工序")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:produce-process:query')")
    public CommonResult<ErpProduceProcessRespVO> getProduceProcess(@RequestParam("id") Long id) {
        ErpProduceProcessDO produceProcess = produceProcessService.getProduceProcess(id);
        return success(BeanUtils.toBean(produceProcess, ErpProduceProcessRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得工艺工序分页")
    @PreAuthorize("@ss.hasPermission('erp:produce-process:query')")
    public CommonResult<PageResult<ErpProduceProcessRespVO>> getProduceProcessPage(@Valid ErpProduceProcessPageReqVO pageReqVO) {
        PageResult<ErpProduceProcessDO> pageResult = produceProcessService.getProduceProcessPage(pageReqVO);
        return success(buildStockOutVOPageResult(pageResult));
    }

    @GetMapping("/list-all")
    @Operation(summary = "获得工艺工序分页")
    @PreAuthorize("@ss.hasPermission('erp:produce-process:query')")
    public CommonResult<List<ErpProduceProcessRespVO>> getProduceProcessList() {
        List<ErpProduceProcessDO> result = produceProcessService.getProduceProcessAll();
        return success(BeanUtils.toBean(result, ErpProduceProcessRespVO.class));
    }


    @GetMapping("/export-excel")
    @Operation(summary = "导出工艺工序 Excel")
    @PreAuthorize("@ss.hasPermission('erp:produce-process:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProduceProcessExcel(@Valid ErpProduceProcessPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ErpProduceProcessDO> list = produceProcessService.getProduceProcessPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "工艺工序.xls", "数据", ErpProduceProcessRespVO.class,
                        BeanUtils.toBean(list, ErpProduceProcessRespVO.class));
    }

    private PageResult<ErpProduceProcessRespVO> buildStockOutVOPageResult(PageResult<ErpProduceProcessDO> pageResult) {
        if (CollUtil.isEmpty(pageResult.getList())) {
            return PageResult.empty(pageResult.getTotal());
        }
        // 1.3 部门信息
        Map<Long, DeptRespDTO> deptMap = deptApi.getDeptMap(
                convertSet(pageResult.getList(), ErpProduceProcessDO::getWorkCenter));
        // 2. 开始拼接
        return BeanUtils.toBean(pageResult, ErpProduceProcessRespVO.class, process -> {
            MapUtils.findAndThen(deptMap, process.getWorkCenter(), dept -> process.setWorkCenterName(dept.getName()));
        });
    }

}